﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using HoangHuy.data;
using System.Linq;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

namespace HoangHuy.form
{
    public partial class frmNhapXuatHangSoLuong : DevExpress.XtraEditors.XtraForm
    {
        public int[] arrRows = null;
        public MobileDataDataContext context = new MobileDataDataContext();
        public int LoaiNhapXuat = 0;
        public DataTable dataInfo = null;

        private DevExpress.XtraGrid.GridControl _grdData;

        public DevExpress.XtraGrid.GridControl grdData
        {
            get { return _grdData; }
            set { _grdData = value; }
        }
      
        private DevExpress.XtraGrid.Views.Grid.GridView _grvData;

        public DevExpress.XtraGrid.Views.Grid.GridView grvData
        {
            get { return _grvData; }
            set { _grvData = value; }
        }
       
        public frmNhapXuatHangSoLuong()
        {
            InitializeComponent();
        }

        public frmNhapXuatHangSoLuong(int value, int []rows)
        {
            arrRows = rows;
            LoaiNhapXuat = value;
            InitializeComponent();
        }

        public void loadrepLoaiHang()
        {
            repLoaiHang.DataSource = context.LoaiHangs;
            repLoaiHang.DisplayMember = "tenLoaiHang";
            repLoaiHang.ValueMember = "id";
            repLoaiHang.View.Columns["tenLoaiHang"].Caption = "Tên Loại Hàng";
            repLoaiHang.View.Columns["id"].Visible = false;
        }

        public void loadDataGridview()
        {
            //dataInfo = Utils.Utils.LINQToDataTable(context.matHangs.Where(q =>q.id.Contains(arrRows));
            var matHang = (from o in context.matHangs
                               where(arrRows.Contains(o.id))
                               select o
                                ).ToList();
            dataInfo = Utils.Utils.LINQToDataTable(matHang);
            foreach(DataRow dr in dataInfo.Rows)
            {
                dr["soLuong"] = 0;
            }
            grdMatHang.DataSource = dataInfo;
            grdData = grdMatHang;
            grvData = grvMatHang;
        }

        private void frmNhapXuatHangSoLuong_Load(object sender, EventArgs e)
        {
            if (LoaiNhapXuat == (int)Utils.enumConstant.LoaiNhapXuat.Nhap)
            {
                lblHeader.Text = "Nhập Hàng";
            }
            else
            {
                lblHeader.Text = "Xuất Hàng";
            }
            loadrepLoaiHang();
            loadDataGridview();
        }

        private void grvMatHang_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.DisplayError;
        }

        private void grvMatHang_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            GridView view = sender as GridView;
            try
            {
                GridColumn colId = view.Columns[0];
                int[] arr = view.GetSelectedRows();
                int Id = int.Parse(view.GetRowCellValue(arr[0], colId).ToString());
                int soLuong = (int)(from o in context.matHangs
                                    where o.id.Equals(Id)
                                    select o.soLuong).Single();
                int luongNhapXuat = int.Parse(e.Value.ToString());
                if (LoaiNhapXuat == (int)Utils.enumConstant.LoaiNhapXuat.Xuat)
                {
                    if (soLuong < luongNhapXuat)
                    {
                        e.ErrorText = "Không đủ số lượng xuất !";
                        e.Valid = false;
                    }
                }
            }
            catch
            {
                e.ErrorText = "bạn phải nhập số !";
                e.Valid = false;
            }
        }
    }
}